package com.spider.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spider.entites.Filed;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author panglin
 * @date 2020/12/30 12:21
 */
public class JsonUtils {

    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static List<Filed> transformJsonToListFiled(String filed) {
        try {
            return OBJECT_MAPPER.readValue(filed, new TypeReference<List<Filed>>() {
                @Override
                public Type getType() {
                    return super.getType();
                }
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String writeValueAsString(Object object){
        try {
            return OBJECT_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
